package com.skit.schedule.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.skit.common.base.BaseFragment
import com.skit.common.bean.ActivityBean
import com.skit.common.bean.StatusResult
import com.skit.common.config.RetrofitClient
import com.skit.common.http.HttpUtils
import com.skit.common.utils.ToastUtils
import com.skit.schedule.R
import com.skit.common.retrofit.ActivityProtocol
import com.skit.schedule.databinding.ScheduleMainFragmentBinding
import kotlinx.android.synthetic.main.schedule_main_fragment.*
import retrofit2.Call
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap

/**
 * @author shuike [Contact me.](mailto:shuike007@gmail.com)
 * @version 1.0
 * @since 2020/5/28
 */

class ScheduleFragment : BaseFragment() {

    private lateinit var adapter: ScheduleRecyclerAdapter
    private val activityList: MutableList<ActivityBean> = ArrayList()
    private lateinit var binding: ScheduleMainFragmentBinding
    public lateinit var scrollGJ: (Int) -> Unit

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = ScheduleMainFragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

    companion object {
        private const val TAG = "ScheduleFragment"
        fun newInstance(): ScheduleFragment {
            val args = Bundle()
            val fragment = ScheduleFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val format = SimpleDateFormat("yyyy年MM月", Locale.getDefault())
//        topbar.setTitleTv(format.format(System.currentTimeMillis()))

        topbar.setTitle(
            resources.getString(
                R.string.select_date,
                calenderView.curYear,
                calenderView.curMonth
            )
        )
        calenderView.setOnMonthChangeListener { y, m ->
            topbar.setTitle(
                resources.getString(
                    R.string.select_date, y, m
                )
            )
        }

        adapter = ScheduleRecyclerAdapter(activityList)
        recyclerView.also {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(context)
            it.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    scrollGJ(dy)
                }
            })
        }
        getData(binding.calenderView.selectedCalendar.timeInMillis)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.calenderView.setOnCalendarSelectListener(object :
            CalendarView.OnCalendarSelectListener {
            override fun onCalendarOutOfRange(calendar: Calendar?) {
                Log.d(TAG, "onCalendarOutOfRange: ${calendar?.timeInMillis}")
            }

            override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
                Log.d(TAG, "onCalendarSelect: ${calendar?.timeInMillis}")
            }
        })
    }

    private fun getData(time: Long) {
        progressBar.visibility = View.VISIBLE
        val userProtocol: ActivityProtocol = RetrofitClient.getService(ActivityProtocol::class.java)
        val call: Call<StatusResult<List<ActivityBean>>> =
            userProtocol.getActivityListByTime(1, time, 1)
        HttpUtils.doRequest(call) {
            progressBar.visibility = View.GONE
            if (it == null) {
                ToastUtils.showToast("请求失败")
                return@doRequest
            }
            if (it.isSuccess) {
                activityList.clear()
                activityList.addAll(it.data)
            }
        }
    }
}